package com.yunheone.Jdbc;

import java.io.FileReader;
import java.sql.*;
import java.util.Properties;

/**
 * Created by Administrator on 2017/8/28 0028.
 */
public class JdbcHelp {
    private static final String URL;
    private static final String DRIVERE;
    private static final String NAME;
    private static final String PWD ;
    private static Connection conn;

    static {
        Properties properties = new Properties ();
        try {
            properties.load ( new FileReader ( "src//Config.properties" ) );
        } catch (Exception e) {
            e.printStackTrace ();
        }
        URL=properties.getProperty ( "url" );
        DRIVERE=properties.getProperty ( "driver" );
        NAME=properties.getProperty ( "name" );
        PWD=properties.getProperty ( "password" );
        try {
            Class.forName ( DRIVERE );
            conn = DriverManager.getConnection ( URL, NAME, PWD );
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }

    public static Connection getConn(){
        return conn;
    }

    /**
     * 增删改
     * @param statement
     */
    public static void Update(PreparedStatement statement) {
        try {
            statement.executeUpdate ();
        } catch (SQLException e) {
            e.printStackTrace ();
        }
    }

    /**
     * 查
     * @param statement
     */
    public static ResultSet Query(PreparedStatement statement){
        ResultSet result=null;
        try {
             result= statement.executeQuery ();
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return result;
    }

    /**
     * 拼装参数，获取执行器
     * @param sql
     * @param pan
     * @return
     */
    public static PreparedStatement item(String sql,Object ...pan){
        PreparedStatement statement=null;
        try {
             statement = conn.prepareStatement ( sql );
            if (pan!=null){
                for (int i=0;i<pan.length;i++){
                    statement.setObject ( i+1,pan[i] );
                }
            }
            return statement;
        } catch (SQLException e) {
            e.printStackTrace ();
            return null;
        }
    }

    /**
     * 关闭结果集
     * @param result
     */
    public static void close(ResultSet result){
        close ( result,null,null );
    }

    /**
     * 关闭执行器
     * @param statement
     */
    public static void close(PreparedStatement statement){
        close ( null,statement,null );
    }

    /**
     * 关闭数据库连接
     * @param conn
     */
    public static void close(Connection conn){
        close (null,null,conn );
    }

    /**
     * 关闭结果集和执行器
     * @param result
     * @param prepared
     */
    public static void close(ResultSet result,PreparedStatement prepared){
        close (result,prepared,null );
    }

    /**
     * 关闭资源
     * @param result
     * @param statement
     * @param conn
     */
    public static void close(ResultSet result,PreparedStatement statement,Connection conn){
       if (result!=null){
           try {
               result.close ();
           } catch (SQLException e) {
               e.printStackTrace ();
           }
       }
       if (statement!=null){
           try {
               statement.close ();
           } catch (SQLException e) {
               e.printStackTrace ();
           }
       }
       if (conn!=null){
           try {
               conn.close ();
           } catch (SQLException e) {
               e.printStackTrace ();
           }
       }
    }
}
